package com.tengniu.web.controller.buss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.buss.domain.MassageChair;
import com.tengniu.buss.domain.MassageChairPackage;
import com.tengniu.buss.domain.MassageChairPoint;
import com.tengniu.buss.service.MassageChairPointService;
import com.tengniu.common.annotation.Log;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import com.tengniu.common.core.controller.BaseController;
import com.tengniu.common.core.domain.AjaxResult;
import com.tengniu.common.core.page.TableDataInfo;
import com.tengniu.common.enums.BusinessType;
import com.tengniu.service.MassageChairAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 按摩投放点管理
 */
@RestController
@RequestMapping("/admin/point")
@Slf4j
public class PointAdminController extends BaseController {

    @Resource
    private MassageChairPointService massageChairPointService;
    @Resource
    private MassageChairAdminService massageChairAdminService;

    /**
     * 获取按摩投放点
     */
    @PreAuthorize("@ss.hasPermi('buss:point:list')")
    @PostMapping("/list")
    public TableDataInfo<MassageChairPoint> list(@RequestBody MassageChairPoint massageChairPoint) {
        startPage();
        List<MassageChairPoint> list = massageChairPointService.list(
                Wrappers.lambdaQuery(MassageChairPoint.class)
                        .eq(Objects.nonNull(massageChairPoint.getStatus()), MassageChairPoint::getStatus, massageChairPoint.getStatus())
                        .like(Objects.nonNull(massageChairPoint.getPointName()), MassageChairPoint::getPointName, massageChairPoint.getPointName()));
        return getDataTable(list);
    }

    /**
     * 新增按摩投放点
     */
    @PreAuthorize("@ss.hasPermi('buss:point:add')")
    @Log(title = "按摩投放点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MassageChairPoint MassageChairPoint) {
        return toAjax(massageChairPointService.save(MassageChairPoint));
    }

    /**
     * 修改按摩投放点
     */
    @PreAuthorize("@ss.hasPermi('buss:package:edit')")
    @Log(title = "按摩投放点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MassageChairPoint MassageChairPoint) {
        return toAjax(massageChairPointService.updateById(MassageChairPoint));
    }

    /**
     * 删除按摩投放点
     */
    @PreAuthorize("@ss.hasPermi('buss:point:remove')")
    @Log(title = "按摩投放点", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{pointId}")
    public AjaxResult remove(@PathVariable Long pointId) {
        //检查是否有关联数据
        boolean flag = massageChairAdminService.checkPoint(pointId);
        if (flag) {
            return AjaxResult.error("该投放点已关联按摩椅套餐，无法删除");
        }
        return toAjax(massageChairPointService.removeById(pointId));
    }

    /**
     * 上架
     */
    @PreAuthorize("@ss.hasPermi('buss:point:shelf')")
    @Log(title = "按摩投放点", businessType = BusinessType.UPDATE)
    @GetMapping("/shelf/{pointId}")
    public AjaxResult shelf(@PathVariable Long pointId) {
        MassageChairPoint massageChairPoint = massageChairPointService.getById(pointId);
        if (massageChairPoint == null) {
            return AjaxResult.error("按摩投放点不存在");
        }
        massageChairPoint.setStatus(YesOrNoEnum.YES.getCode());
        return toAjax(massageChairPointService.updateById(massageChairPoint));
    }

    /**
     * 下架
     */
    @PreAuthorize("@ss.hasPermi('buss:point:offShelf')")
    @Log(title = "按摩投放点", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelf/{pointId}")
    public AjaxResult offShelf(@PathVariable Long pointId) {
        MassageChairPoint massageChairPoint = massageChairPointService.getById(pointId);
        if (massageChairPoint == null) {
            return AjaxResult.error("按摩投放点不存在");
        }
        //检查是否有关联数据
        boolean flag = massageChairAdminService.checkPoint(pointId);
        if (flag) {
            return AjaxResult.error("该投放点已关联按摩椅套餐，无法下架");
        }
        massageChairPoint.setStatus(YesOrNoEnum.NO.getCode());
        return toAjax(massageChairPointService.updateById(massageChairPoint));
    }

}
